package spacegame.model;

import org.andengine.entity.primitive.Line;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.DrawType;

import spacegame.interfaces.ISpaceObject;
import spacegame.interfaces.ISpaceWorldContainer;
import spacegame.resources.ResourceFactory;

import android.annotation.SuppressLint;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.MassData;

/**
*
* @author Muiota Betarho
* @since 18:48:00 - 21.10.2012
*/
@SuppressLint("FloatMath")
public class SpaceShipItem implements ISpaceObject{
	
	
	private ITextureRegion m_spaceShipTextureRegion;
	private ITiledTextureRegion mToggleButtonTextureRegion;
	private Body m_playerBody;
	private AnimatedSprite m_mainEngineFireLeftSprite;
	private AnimatedSprite m_mainEngineFireRightSprite;
	private AnimatedSprite m_mainEngineFireCenterSprite;
	private AnimatedSprite m_rotateEngineFireLeftSprite;
	private AnimatedSprite m_rotateEngineFireRightSprite;
	private AnimatedSprite m_mainEngineForwardSprite;
	private int m_lastCurrentAnimateDuration;
	public Vector2 ThrustVelocity = Vector2Pool.obtain(0,0);
	public Vector2 TieRodVelocity= Vector2Pool.obtain(0,0);
	public Vector2 TieCenter= Vector2Pool.obtain(0,0);
	public Vector2 BodyCenter= Vector2Pool.obtain(0,0);
	private static final float[] _bufferPoints = new float[2];
	private Line m_line;
	private Sprite m_face;
	
	public SpaceShipItem(int pX, int pY, ResourceFactory pResorceFactory, ISpaceWorldContainer pMainActivity)
	{
		m_spaceShipTextureRegion = pResorceFactory.GetShipTexture();
		mToggleButtonTextureRegion = pResorceFactory.GetFireSprite();
		//Create main ship
		 m_face = new Sprite(pX, pY, this.m_spaceShipTextureRegion, pMainActivity.getVertexBufferObjectManager());	
		m_face.setScale(0.5f);
		final FixtureDef _objectFixtureDef = PhysicsFactory.createFixtureDef( 1f, 0.3f, 1f);
		m_playerBody = PhysicsFactory.createBoxBody(pMainActivity.GetCurrentPhysicsWorld(), m_face, BodyType.DynamicBody, _objectFixtureDef);
		
		pMainActivity.GetCurrentScene().attachChild(m_face);
		pMainActivity.GetCurrentPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(m_face, m_playerBody, true, true));
		pMainActivity.GetCurrentCamera().setChaseEntity(m_face);
		
		
		// Add engine fire sprite
		m_mainEngineFireLeftSprite = createFireSprite(pMainActivity, -6, 56,
				180);
		m_face.attachChild(m_mainEngineFireLeftSprite);
		m_mainEngineFireRightSprite = createFireSprite(pMainActivity, 10, 56,
				180);
		m_mainEngineFireRightSprite.setCurrentTileIndex(8);
		m_face.attachChild(m_mainEngineFireRightSprite);
		m_mainEngineFireCenterSprite = createFireSprite(pMainActivity, 2, 53,
				180);
		m_mainEngineFireCenterSprite.setCurrentTileIndex(16);
		m_face.attachChild(m_mainEngineFireCenterSprite);

		m_rotateEngineFireLeftSprite = createFireSprite(pMainActivity, -14, 2,
				235);
		m_rotateEngineFireLeftSprite.setScale(0.8f);
		m_face.attachChild(m_rotateEngineFireLeftSprite);
		
		m_rotateEngineFireRightSprite = createFireSprite(pMainActivity, 18, 2,
				125);
		m_rotateEngineFireRightSprite.setScale(0.8f);
		m_face.attachChild(m_rotateEngineFireRightSprite);
		
		
		
		m_mainEngineForwardSprite = createFireSprite(pMainActivity, 2, -22, 0);
		m_face.attachChild(m_mainEngineForwardSprite);
		
		final MassData _bodyMassData = GetMassData();
		_bodyMassData.mass = 10;
		_bodyMassData.I = 10;
		
		SetMassData(_bodyMassData);
		
		
		final Vector2 _bodyCenter = GetWorldCenter();		
		m_line = new Line(_bodyCenter.x, _bodyCenter.y, _bodyCenter.x, _bodyCenter.y+100, 1, pMainActivity.getVertexBufferObjectManager(), DrawType.DYNAMIC);
		m_line.setColor(0, 1, 0);
		m_line.setVisible(true);

		pMainActivity.GetCurrentScene() .attachChild(m_line);
		
		
	}

	private AnimatedSprite createFireSprite( ISpaceWorldContainer pMainActivity, int pX, int pY, int pAngle) {
		final AnimatedSprite _animatedSprite = new AnimatedSprite(pX, pY, this.mToggleButtonTextureRegion, pMainActivity.getVertexBufferObjectManager());
			_animatedSprite.animate(20);
			_animatedSprite.setZIndex(-1);
			_animatedSprite.setRotation(pAngle);
		return _animatedSprite;
	}
	
	public double GetAngle() {
		return m_playerBody.getAngle();
	}

	public Vector2 GetWorldCenter() {
		return m_playerBody.getWorldCenter();
	}

	public MassData GetMassData() {
		return m_playerBody.getMassData();
	}

	public void SetMassData(MassData _bodyMassData) {
		m_playerBody.setMassData(_bodyMassData);
		
	}

	/** Приложить силу к объекту
	 * @param pForce - сила
	 * @param _pCenter - точка приложения
	 */
	public void ApplyForce(Vector2 pForce, Vector2 _pCenter) {
		m_playerBody.applyForce(pForce, _pCenter);
		
	}

	@SuppressLint("FloatMath")
	public void ApplyEngineForce(final float pValueX, final float pValueY) {
		final float _theta = (float)(GetAngle() + Math.PI*0.5);		
		final float _deltax = (float) (Math.cos(_theta));
		final float _deltay = (float) (Math.sin(_theta));
		final Vector2 _getWorldCenter = m_playerBody.getWorldCenter();
		BodyCenter.x = _getWorldCenter.x;
		BodyCenter.y = _getWorldCenter.y;
	

		

		/*_thrustVelocity = null;
		_tieRodVelocity = null;
		_tieCenter = null;*/
		
	 if (pValueX > 0.1)
	{
		m_rotateEngineFireLeftSprite.setVisible(true);
		m_rotateEngineFireLeftSprite.setScale(pValueX);
		m_rotateEngineFireRightSprite.setVisible(false);
		final float _xRadius =  pValueX * 3;
		 TieRodVelocity.x  = _xRadius*_deltay ;
		 TieRodVelocity.y  =  -_xRadius*_deltax ;
		 TieCenter.x = _getWorldCenter.x-_deltax*4;
		 TieCenter.y = _getWorldCenter.y-_deltay*4 ;
	}
	else  if (pValueX < -0.1)
	{
		m_rotateEngineFireLeftSprite.setVisible(false);
		m_rotateEngineFireRightSprite.setVisible(true);
		m_rotateEngineFireRightSprite.setScale(-pValueX);
		final float _xRadius =  pValueX * 3;
		TieRodVelocity.x = _xRadius*_deltay ;
		TieRodVelocity.y = -_xRadius*_deltax ;
		TieCenter.x =  _getWorldCenter.x-_deltax*4;
		TieCenter.y = _getWorldCenter.y-_deltay*4 ;
		
	}
	else 
	{
		m_rotateEngineFireLeftSprite.setVisible(false);
		m_rotateEngineFireRightSprite.setVisible(false);
		 TieRodVelocity.x = 0;
		 TieRodVelocity.y =  0 ;
		 TieCenter.x = _getWorldCenter.x-_deltax*4;
		 TieCenter.y = _getWorldCenter.y-_deltay*4;
		
	} 
	
	 if (pValueY > 0.1)
		{
			m_mainEngineFireLeftSprite.setVisible(false);
			m_mainEngineFireRightSprite.setVisible(false);
			m_mainEngineFireCenterSprite.setVisible(false);
			m_mainEngineForwardSprite.setVisible(true);
			final float _shiftY = pValueY * 8;
			m_mainEngineForwardSprite.setY(-14 - _shiftY);
			m_mainEngineForwardSprite.setScaleY(pValueY);
			final float _yRadius =  pValueY * 10;
			ThrustVelocity.x = _yRadius*_deltax ;
			ThrustVelocity.y = _yRadius*_deltay ;
		}
	 else   if (pValueY < -0.1)
	 {
		 m_mainEngineFireLeftSprite.setVisible(true);
			m_mainEngineFireRightSprite.setVisible(true);
			m_mainEngineFireCenterSprite.setVisible(true);
			m_mainEngineForwardSprite.setVisible(false);
			m_mainEngineFireLeftSprite.setScaleY(-pValueY);
			m_mainEngineFireRightSprite.setScaleY(-pValueY);
			m_mainEngineFireCenterSprite.setScaleY(-pValueY);
			
			
			final float _shiftY = pValueY * 8;
			final int _currentAnimateDuration = 20+ (int)_shiftY;
			if (m_lastCurrentAnimateDuration != _currentAnimateDuration)
			{
				m_lastCurrentAnimateDuration = _currentAnimateDuration;
			m_mainEngineFireLeftSprite.animate(_currentAnimateDuration);
			m_mainEngineFireRightSprite.animate(_currentAnimateDuration+1);
			m_mainEngineFireCenterSprite.animate(_currentAnimateDuration+2);
			}
			m_mainEngineFireLeftSprite.setY(48-_shiftY);
			m_mainEngineFireRightSprite.setY(48-_shiftY);
			m_mainEngineFireCenterSprite.setY(45-_shiftY);
			final float _yRadius =  pValueY * 20;
			 ThrustVelocity.x = _yRadius*_deltax ;
			 ThrustVelocity.y = _yRadius*_deltay ;
	 }
	 else
	 {
		 
		 m_mainEngineFireLeftSprite.setVisible(false);
			m_mainEngineFireRightSprite.setVisible(false);
			m_mainEngineFireCenterSprite.setVisible(false);
			m_mainEngineForwardSprite.setVisible(false);
			//final float _yRadius =  pValueY * 25;
			ThrustVelocity.x = 0 ;
			ThrustVelocity.y = 0;
	 }
	 
	 
	//engine thrust

	/*float _angularImpulse = pValueX * 2;
	if (_angularImpulse > 0.4 || _angularImpulse < -0.4)
		m_testbody.applyAngularImpulse(_angularImpulse);*/
		
	}

	public float[] ConvertLocalToGlobal(Vector2 vector2) {
		_bufferPoints[0] = vector2.x;
		 _bufferPoints[1] = TieCenter.y;
		 final float[] _pls =m_face.convertLocalToSceneCoordinates(_bufferPoints);
		return _pls;
	}

	@Override
	public void Update(float pSecondsElapsed) {
		ApplyForce(ThrustVelocity,BodyCenter);
		ApplyForce(TieRodVelocity, TieCenter);
		final float[] _pls = ConvertLocalToGlobal(TieCenter);	 
		 m_line.setPosition(_pls[0], _pls[1],BodyCenter.x, BodyCenter.y);
		
	}

	@Override
	public Vector2 GetPosition() {
		return m_playerBody.getWorldCenter();
	}

	@Override
	public int GetMass() {
		// TODO Auto-generated method stub
		return 0;
	}
	

	
	
}
